//
//  MantisLeaveMessageViewController.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/4/30.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "MantisLeaveMessageViewController.h"
#import "ShowMessageViewController.h"
#import "MantisChatHeadView.h"
#import "ChatLeaveMessageView.h"
#import "ManitsFileResultModel.h"
#import "MatinsIMinitModel.h"
#import "CommonHeader.h"
#import "MantisRequestManager.h"



@interface MantisLeaveMessageViewController ()
@property(nonatomic,strong)MatinsIMinitModel*leaveModel;
@property(nonatomic,strong)ChatLeaveMessageView*messageView;
@property(nonatomic,strong)ShowMessageViewController*showVC;
@property(nonatomic,copy)NSString*phoneRegx;//
@end

@implementation MantisLeaveMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     
    [self setNavigationBarStyle:BarTint_Default];
    [self setNavigationTitle:@"在线留言"];
    self.view.backgroundColor =kDefaultBgColor;

    MantisChatHeadView*headView = [[MantisChatHeadView alloc]initWithFrame:CGRectMake(0,0, IMSCREEN_WIDTH, mantisHeadViewHeight)];
    [self.view addSubview:headView];
    
    
    
    ChatLeaveMessageView*messageView= [[ChatLeaveMessageView alloc]initWithFrame:CGRectZero];
    self.messageView=messageView;
    [self.view addSubview:messageView];
    [messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(headView.mas_bottom);
        make.height.mas_equalTo(adaptWidth750(218*2));
    }];
    


    UIButton*sumitBtn = [UIButton new];
    sumitBtn.titleLabel.font=KIMFontRegularSize(13);
    [sumitBtn setTitle:@"提交留言" forState:UIControlStateNormal];
    [sumitBtn addTarget:self action:@selector(sumitLeaVeMessage:) forControlEvents:UIControlEventTouchUpInside];
    sumitBtn.backgroundColor=KAppMainColor;
    [sumitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:sumitBtn];
    CGFloat leftSpace =adaptWidth750(30);
    CGFloat sumitWidth=IMSCREEN_WIDTH-leftSpace*2;
    [sumitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageView.mas_bottom).offset(adaptWidth750(80));
        make.width.mas_equalTo(sumitWidth);
        make.height.mas_equalTo(adaptWidth750(100));
        make.centerX.mas_equalTo(0);
    }];
    ViewRadius(sumitBtn, adaptWidth750(10));
    
    self.leaveModel =[MantisSDKTool keyManitsSDKTool].myModel;

}

/**
* 功能：校验手机号码
*/
- (BOOL)validateMobile:(NSString *)mobileNumber
{
    if ([NSString isBlankString:mobileNumber]) {
        return  false;
    }else{
        NSPredicate*regextmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",self.phoneRegx];
        if ([regextmobile evaluateWithObject:mobileNumber]) {
            return  YES;
        }
        return NO;
    }
}

-(void)sumitLeaVeMessage:(UIButton*)sender{
    [self.view endEditing:YES];
    NSString*nameStr=self.messageView.nameInputView.inputView.text;
    NSString*phoneStr=self.messageView.phoneInputView.inputView.text;
   
    if ([NSString isBlankString:phoneStr]) {
        [self.view nlShowHudText:@"请输入手机号" hideDelay:2.0 completionBlock:nil];
        return;
    }
    
    if (![self validateMobile:phoneStr]) {
        [self.view nlShowHudText:@"手机号不正确" hideDelay:2.0 completionBlock:nil];
        return;
    }
    
    NSMutableDictionary*dic=[NSMutableDictionary new];
    dic[@"name"]=nameStr;
    dic[@"phone1"]=phoneStr;
    dic[@"content"]=self.messageView.textView.leaveMessageView.text;
    dic[@"uid"]=self.leaveModel.uuidStr;
    dic[@"buId"]=self.leaveModel.buId;
    dic[@"probeId"]=self.leaveModel.probeId;
    dic[@"companyId"]=self.leaveModel.companyId;
    dic[@"projectId"]=self.leaveModel.projectId;
    dic[@"reqVistorMedia"]=@"mobile";
    dic[@"lpUrl"]=lpUrlStr;
 
    NSString*webUrl =@"https://";
    NSString*chatSever=[NSString isBlankString:self.leaveModel.chatServer]?@"test.bjmantis.net":self.leaveModel.chatServer;
    NSString*lineStr=@"/";
    NSNumber*companyID=!self.leaveModel.companyId?@(7011):self.leaveModel.companyId;
    NSString*requestStr=@"/api-war/message/insertMessageInfo2.do";

    NSString*requestUrlStr=[NSString stringWithFormat:@"%@%@%@%@%@",webUrl,chatSever,lineStr,companyID,requestStr];
    NSLog(@"requestUrlStr-----%@",requestUrlStr);
    WS(weakSelf);
    [MantisRequestManager pureRequestWithUrl:requestUrlStr method:POST params:dic headerField:nil progress:^(NSProgress *progress) {
        
    } onSuccess:^(id data, NSURLSessionDataTask *task) {
        ManitsFileResultModel*sumitModel=[ManitsFileResultModel mj_objectWithKeyValues:data];
        if ([sumitModel.flag isEqualToString:@"1"]) {
            ShowMessageViewController*showVC =[[ShowMessageViewController alloc]initWithArr:@[@"确定"]];
            weakSelf.showVC=showVC;
            showVC.messageLabel.text=@"留言成功，咨询师会尽快与您联系";
            showVC.cancleBlock  = ^{
                [weakSelf.showVC dismissFromParentVC];
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            };
            [showVC showCurrentVC:self.navigationController];
        }else
        {
            [weakSelf.view nlShowHudText:@"提交失败" hideDelay:2.0 completionBlock:nil];
        }
    
    } onFailure:^(NSError *error, NSURLSessionTask *task) {
         [weakSelf.view nlShowHudText:@"提交失败" hideDelay:2.0 completionBlock:nil];
    }];

}


-(void)backAction:(UIButton *)btn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotate{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
